package vn.fpt.snake.controller;

import java.awt.Image;
import java.awt.Point;
import java.util.List;
import vn.fpt.snake.commons.ChiliThread;
import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.commons.Utility;
import vn.fpt.snake.commons.WatermelonThread;
import vn.fpt.snake.entity.AbstractFruit;
import vn.fpt.snake.entity.Apple;
import vn.fpt.snake.entity.Chili;
import vn.fpt.snake.entity.Watermelon;

public class FruitControllerImpl extends AbstractController {
	Utility utility;
	GameControllerImpl gameController;

	public FruitControllerImpl() {
		super();
		utility = new Utility();
		gameController = new GameControllerImpl();
	}

	public Apple createNewApple() throws Exception {
		Image appleIcon = gameController.getImage(Constants.APPLE_IMG);
		Point point;
		Apple apple = null;
		point = utility.randomPoint();
		apple = new Apple();
		apple.setX(point.x);
		apple.setY(point.y);
		apple.setIcon(appleIcon);
		return apple;
		//Dead code, vi o tren anh gan gia tri cho Apple roi nen ko ba
		//else{
			//throw new Exception("Apple is null");
		//}
	}

	@SuppressWarnings("unused")
	public Watermelon createNewWatermelon() throws Exception {
		Watermelon watermelon = new Watermelon();
		watermelon.setX(-1);
		watermelon.setY(-1);
		watermelon.setIcon(null);
		if (watermelon != null) {
			return watermelon;
		} else
			throw new Exception("Water melon is null");
	}

	@SuppressWarnings("unused")
	public Chili createNewChili() throws Exception {
		Chili chili = new Chili();
		chili.setX(-1);
		chili.setY(-1);
		chili.setIcon(null);
		if (chili != null) {
			return chili;
		} else {
			throw new java.lang.Exception("Chili is null");
		}

	}

	public void locateWatermelon(List<AbstractFruit> fruits) throws Exception {
		if (fruits != null) {
			boolean isFound = false;
			for (AbstractFruit fruit : fruits) {
				if (fruit instanceof Watermelon) {
					if (fruit.getX() == -1 && fruit.getY() == -1) {
						Image watermelonIcon = gameController.getImage(Constants.WATERMELON_IMG);
						Point newPoint = utility.randomPoint();
						fruit.setX(newPoint.x);
						fruit.setY(newPoint.y);
						deleteWatermelon(fruits);
						fruit.setIcon(watermelonIcon);
						isFound = true;	
					}			
					if(isFound){
						break;
					}
				} 
			}
			if(!isFound)
				throw new Exception("List fruits does not contain watermelon object");
		} else {
			throw new Exception("List fruits is null");
		}

	}

	public void locateChili(List<AbstractFruit> fruits) throws Exception{
		if (fruits != null) {
			boolean isFound = false;
			for (AbstractFruit fruit : fruits) {
				if (fruit instanceof Chili) {
					if (fruit.getX() == -1 && fruit.getY() == -1) {
						Image chiliIcon = gameController.getImage(Constants.CHILI_IMG);
						Point newPoint = utility.randomPoint();
						fruit.setX(newPoint.x);
						fruit.setY(newPoint.y);
						deleteChili(fruits);
						fruit.setIcon(chiliIcon);
						isFound = true;
					}
					if(isFound){
						break;
					}
				}
			}
			if(!isFound)
				throw new Exception("List fruits does not contain chili object");
		} else {
			throw new Exception("List fruits is null");
		}
	}

	public void deleteWatermelon(List<AbstractFruit> fruits) {
		Thread thread = new WatermelonThread(fruits);
		thread.start();

	}

	public void deleteChili(List<AbstractFruit> fruits) {
		Thread thread = new ChiliThread(fruits);
		thread.start();
	}
}
